package com.platform.modules.prj.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.BeanUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.entity.TreeNode;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.CadUtils;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.config.DocConstants;
import com.platform.entity.Attachment;
import com.platform.modules.prj.dto.TreeNodeVo;
import com.platform.modules.prj.dto.WorkareaDrawingTreeByAppDto;
import com.platform.modules.prj.dto.WorkareaListByAppDto;
import com.platform.modules.prj.dto.WrokareaDrawingListByAppDto;
import com.platform.modules.prj.entity.PrjWorkAreaEntity;
import com.platform.modules.prj.service.PrjWorkAreaService;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.util.DocUtil;
import com.platform.web.utils.CommonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@Api(tags = "PrjSuperviseController|项目监管")
@RestController
@RequestMapping("prj/workarea")
public class PrjWorkAreaController extends AbstractController {

    @Autowired
    private PrjWorkAreaService prjWorkAreaService;
    
    @Autowired
    DocUtil docUtil;
  
    @ApiOperation(value = "保存分区", notes = "保存分区")
    @PostMapping("/save")
    public RestResponse save(PrjWorkAreaEntity prjWorkAreaEntity) {
    	SysUserEntity sysUserEntity = getUser();
    	prjWorkAreaService.save(prjWorkAreaEntity, sysUserEntity);
        return RestResponse.success();
    }
    
    @ApiOperation(value = "删除分区", notes = "删除分区")
    @DeleteMapping("/remove")
    public RestResponse remove(@RequestParam(required = false) String areaIds) {
    	if(!StringUtils.isNullOrEmpty(areaIds)) {
    		prjWorkAreaService.removeByIds(Arrays.asList(areaIds.split(",")));
    	}
        return RestResponse.success();
    }
    
    @ApiOperation(value = "删除分区", notes = "删除分区")
    @DeleteMapping("/remove/{areaId}")
    public RestResponse removeAreaId(@PathVariable("areaId") String areaId) {
    	if(!StringUtils.isNullOrEmpty(areaId)) {
    		prjWorkAreaService.removeByIds(Arrays.asList(areaId.split(",")));
    	}
        return RestResponse.success();
    }
    

	@ApiOperation(value = "查询项目工区，prjId为空则查询当前项目工区", notes = "查询本项目工区")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = "query", name = "prjId", value = "项目ID", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "parentId", value = "父工区ID,0:只查父工区", dataType = "String"),
		@ApiImplicitParam(paramType = "query", name = "zy", value = "专业", dataType = "String"),
   })
   @GetMapping("/list")
   public RestResponse query(
   		@RequestParam(required = false) String areaName,    		
   		@RequestParam(required = false) Integer currPage, 
   		@RequestParam(required = false) Integer limit, 
   		@RequestParam(required = false) String prjId, 
   		@RequestParam(required = false) String parentId) {
		Map<String, Object> params = new HashMap<>();
       params.put("areaName", areaName);
	   //params.put("page", PageUtil.getCurrPage(currPage));
	   //params.put("limit", PageUtil.getLimit(limit));
	   //查询
	   params.put("prjId", StringUtils.isNotBlank(prjId)? prjId: getOrgNo());
	   params.put("parentId", parentId);
	   List<PrjWorkAreaEntity> list = prjWorkAreaService.queryList(params);
	   return RestResponse.success().put("data", list);
   }
    
    @ApiOperation(value = "上传图纸/附件", notes = "上传图纸/附件")
    @PostMapping("/upload")
    public RestResponse upload( MultipartFile file, String dirId, String docId) throws IOException {
    	prjWorkAreaService.saveDrawing(file, dirId, docId);
    	return RestResponse.success();
    }
    
    
    @ApiOperation(value = "工区图纸目录树", notes = "工区图纸目录树")
    @GetMapping("/drawingDirTree")
    public RestResponse drawingDirTree(String areaId) {
    	List<TreeNode> list = docUtil.getDirTreeByDocBusinessIdAndDocCode(areaId, "WORK_AREA_DRAWING");
    	return RestResponse.success().put("result", list);
    }
    
    
    @ApiOperation(value = "工区图纸树", notes = "工区图纸目录树")
    @GetMapping("/drawingTree")
    public CommonResult<List<TreeNode>> drawingTree(String areaId) {
    	List<TreeNode> tree = docUtil.getDocTreeByDocBusinessIdAndDocCode(areaId, "WORK_AREA_DRAWING");
    	return CommonResult.success(tree);
    }
    
    @ApiOperation(value = "工区图纸目录树", notes = "工区图纸目录树")
    @GetMapping("/drawing")
    public RestResponse drawing(String areaId, String dirId) {
    	List<Attachment> list = null;
    	
    	if(dirId!=null && !"".equals(dirId)) {
    		list = docUtil.getAttListByDirId(dirId);
    	}else {
    		list = docUtil.getAttListByDocBusinessIdAndDocCode(areaId,"WORK_AREA_DRAWING");
    	}
    	return RestResponse.success().put("list", list);
    }
    
    
    @ApiOperation(value = "删除图纸", notes = "删除图纸")
    @PostMapping("/removeDrawing")
    public RestResponse removeDrawing( String ids) {
    	if(!StringUtils.isNullOrEmpty(ids)) {
    		docUtil.deleteAtts(Arrays.asList(ids.split(",")));
    	}
    	return RestResponse.success();
    }
    
    @ApiOperation(value = "查询本项目工区App", notes = "查询本项目工区App")
    @PostMapping("/listByApp")
    public RestResponse queryByApp(@RequestBody WorkareaListByAppDto workareaListByAppDto) {
    	Map<String, Object> params = new HashMap<>();
        //params.put("page", PageUtil.getCurrPage(workareaListByAppDto.getCurrPage()));
        //params.put("limit", PageUtil.getLimit(workareaListByAppDto.getLimit()));
        params.put("prjId", workareaListByAppDto.getPrjId());
        params.put("parentId", workareaListByAppDto.getParentId());
    	List<PrjWorkAreaEntity> list = prjWorkAreaService.queryList(params);
        return RestResponse.success().put("data", list);
    }

    @ApiOperation(value = "工区图纸目录树app", notes = "工区图纸目录树app")
    @PostMapping("/drawingDirTreeByApp")
    public RestResponse drawingDirTreeByApp(@RequestBody WorkareaDrawingTreeByAppDto drawingTreeByAppDto) {
    	List<TreeNode> list = docUtil.getDirTreeByDocBusinessIdAndDocCode(drawingTreeByAppDto.getAreaId(), "WORK_AREA_DRAWING");
    	return RestResponse.success().put("data", list);
    }
    
    
    
    
    
    @ApiOperation(value = "工区图纸列表app", notes = "工区图纸列表app")
    @PostMapping("/drawingListApp")
    public RestResponse drawingListByApp(@RequestBody WrokareaDrawingListByAppDto drawingListByAppDto) {
    	String dirId = drawingListByAppDto.getDirId();
    	String areaId = drawingListByAppDto.getAreaId();
    	List<Attachment> list = null;
    	
    	if(dirId!=null && !"".equals(dirId)) {
    		list = docUtil.getAttListByDirId(dirId);
    	}else {
    		list = docUtil.getAttListByDocBusinessIdAndDocCode(areaId,"WORK_AREA_DRAWING");
    	}
    	return RestResponse.success().put("data", list);
    }
    
    @ApiOperation(value = "工区图纸目录树app(新增图纸列表)", notes = "工区图纸目录树app(新增图纸列表)")
    @PostMapping("/drawingDirTreeByApp2")
    public RestResponse drawingDirTreeByApp2(@RequestBody WorkareaDrawingTreeByAppDto drawingTreeByAppDto) {
    	List<TreeNode> list = docUtil.getDirTreeByDocBusinessIdAndDocCode(drawingTreeByAppDto.getAreaId(), "WORK_AREA_DRAWING");
    	List<TreeNodeVo> vo = new LinkedList<TreeNodeVo>(); 
    	if(null!=list && list.size()>0) {
    		TreeNode root = list.get(0);
    		if(null!=root && null!=root.getChildren()) {
    			List<TreeNode> childrenList = root.getChildren();	
	    		for(int i=0; i<childrenList.size(); i++) {
	    			TreeNode treeNode = childrenList.get(i);
	    			TreeNodeVo treeNodeVo = new TreeNodeVo();
	    			org.springframework.beans.BeanUtils.copyProperties(treeNode, treeNodeVo);
	    			String dirId = treeNode.getId();
	    			String areaId = drawingTreeByAppDto.getAreaId();
	    			List<Attachment> atts = null;
	    			//查询文件列表
	    			if(dirId!=null && !"".equals(dirId)) {
	    				atts = docUtil.getAttListByDirId(dirId);
	    	    	}else {
	    	    		atts = docUtil.getAttListByDocBusinessIdAndDocCode(areaId,"WORK_AREA_DRAWING");
	    	    	}
	    			treeNodeVo.setAtts(atts);
	    			vo.add(treeNodeVo);
	    		}
    		}   
    	}
    	return RestResponse.success().put("data", vo);
    }
}
